package com.iot08.api.service;

import com.iot08.common.constant.Const;
import com.iot08.common.entity.app.Knowledge;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2020/12/25 15:51
 */
@Slf4j
@Service
public class KnowledgeService extends BaseService<Knowledge> {

    /**
     * @time: 2020/12/25 17:32
     * @author: Rock
     * @version 1.0
     */
    public ResponseMap getInfo() {
        Query query = new Query(Criteria.where("deleted").is(0));
        List<Knowledge> knowledgeList = this.find(query.with(Sort.by(Sort.Order.asc("index"))));
        List<Map<String, Object>> data = new ArrayList<>();
        for (Knowledge knowledge : knowledgeList) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", knowledge.getId());
            temp.put("title", knowledge.getName());
            if (new Integer(2).equals(knowledge.getType())) {
                temp.put("type", "WEB");
                temp.put("url", knowledge.getWebUrl());
            } else if (Const.INT_1.equals(knowledge.getType())) {
                temp.put("type", knowledge.getSdkMark());
            }
            data.add(temp);
        }
        ResponseMap res = ResponseMap.success();
        res.put("knowledgeColumns", data);
        return res;
    }


}
